package com.zk.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.logging.log4j.Log4jImpl;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.spring.annotation.MapperScan;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;

/**
 * @author cyt
 * @create 2021-03-19 10:12
 */
@Slf4j
@MapperScan("com.zk")
@Configuration
public class MybatisConfig {


    /**
     * 配置 Mybatis 的数据源
     *
     * @return
     */
    @Bean
    public DruidDataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("cui0411..");
        druidDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/ssm?characterEncoding=utf-8&serverTimezone=GMT&useSSL=false");
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        return druidDataSource;
    }

    /**
     * 配置 spring 的事务管理器
     *
     * @param druidDataSource
     * @return
     */
    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager(DruidDataSource druidDataSource) {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(druidDataSource);
        return dataSourceTransactionManager;
    }

    /**
     * 配置 mybatis
     *
     * @param dataSource
     * @return
     * @throws IOException
     */
    @Bean
    public SqlSessionFactoryBean SqlSessionFactory(@Autowired DruidDataSource dataSource) throws IOException {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Interceptor[] plugins = new PageInterceptor[1];

        // tk.Mapper
        tk.mybatis.mapper.session.Configuration configuration = new tk.mybatis.mapper.session.Configuration();
        configuration.setMapperHelper(new MapperHelper());
        // 添加打印日志
        configuration.setLogImpl(StdOutImpl.class);

        //配置分页插件
        PageInterceptor pageInterceptor = new PageInterceptor();
        Properties props = new Properties();
        props.setProperty("helperDialect", "mysql");
        props.setProperty("supportMethodsArguments", "true");
        props.setProperty("rowBoundsWithCount", "true");
        pageInterceptor.setProperties(props);
        ArrayList<PageInterceptor> pageInterceptors = new ArrayList<PageInterceptor>();
        pageInterceptors.add(pageInterceptor);

        factory.setConfiguration(configuration);
        factory.setPlugins(pageInterceptors.toArray(plugins));

        // factory.setConfigLocation(resolver.getResource("classpath:mybatis.xml"));
        factory.setMapperLocations(resolver.getResources("classpath:UserMapper.xml"));
        factory.setDataSource(dataSource);
        return factory;
    }
}
